package com.product.android.utils.wbAtUtils;

import com.product.android.utils.MyPasteListener;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

/**
 * 用于微博at重名功能，输入框监听，将at人名转换为图片展示
 * 
 * <br>Created 2013-12-12 下午4:45:19
 * @version  v1.3.2
 * @author   huangyx		
 *
 * @see 	 
 * 
 * Copyright(c) 2009-2014, TQ Digital Entertainment, All Rights Reserved
 *
 */
public class WbAtTextWatcher implements TextWatcher {

	private Context mContext;
	private EditText mEditText;

	private MyPasteListener mPasteListener;

	public WbAtTextWatcher(Context context, EditText editText) {
		mContext = context;
		mEditText = editText;
	}

	public void setPasteListener(MyPasteListener listener) {
		mPasteListener = listener;
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {

	}

	@Override
	public void afterTextChanged(Editable s) {
		if (mPasteListener == null) {
			return;
		}
		if (mPasteListener.getIsDoPaste()) {
			WbAtView.setSpanAtName(mContext, s, (int) mEditText.getTextSize());
			mPasteListener.setIsDoPaste(false);
		}
		int selection = mPasteListener.getSelection();
		if (selection < 0) {
			mPasteListener.setSelection(0);
		} else if (selection > s.length()) {
			mPasteListener.setSelection(s.length());
		}
		mEditText.setSelection(mPasteListener.getSelection());
	}

}
